package com.xinyue.user.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xinyue.user.pojo.Message;
import com.xinyue.user.service.Impl.MessageServiceImpl;
import com.xinyue.user.service.Impl.UserServiceImpl;

@RestController
public class MessageController {

	@Autowired
	private MessageServiceImpl messageService;
	
	@Autowired
	private UserServiceImpl userService;
	
	@RequestMapping("/insertMess")
	public void insertMess(Message message,HttpSession session) {
		try {
			//设置发送信息的时间
			java.sql.Date time= new java.sql.Date(new Date().getTime());
			message.setCreated(time);
			//设置发送信息发送人的id
			//User user = (User)session.getAttribute("username");
			//message.setSid(user.getId());
			message.setSid(15);
			messageService.insert(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 
	 * @param sid  发送者的id
	 * @param rid  接受者的id
	 * @return
	 */
	@RequestMapping("/showMessages")
	public Message showMessages(HttpSession session,Integer sid,Integer rid) {
		Message mess = null;
		try {
			//User user = (User)session.getAttribute("usename");user.getId()
			mess = messageService.selectById(sid, rid);
			String nickname = userService.selectByPrimaryKey(sid).getNickname();
			Date date = mess.getCreated();//得到数据库的时bai间
			//修改时间并展示（未完成）
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
			String s = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mess;
	}
	
	
	
	
	
	
}
